# -*- coding=utf-8
import json

from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos.cos_exception import CosClientError, CosServiceError
import sys
import logging
# from app.config.setting import PERSONAL_PATH
from app.oss.sts import Sts, Scope, CIScope


class TencentOss(object):

    def __init__(self):
        logging.basicConfig(level=logging.INFO, stream=sys.stdout)
        self.secret_id = 'AKIDMWR5SYOxFI6dtsa2D7k7C0XooXzFFyoN'
        self.secret_key = 'LPCiyxWt1uXu1tWt0sTnreAQP7ZkeHek'
        region = 'ap-nanjing'
        token = None
        scheme = 'https'
        config = CosConfig(Region=region, SecretId=self.secret_id, SecretKey=self.secret_key, Token=token,
                           Scheme=scheme)
        self.client = CosS3Client(config)

    def upload(self, file, osskey):
        response = self.client.upload_file(
            Bucket='personal-documents-1309903013',
            Key=osskey,
            LocalFilePath=file,
            EnableMD5=False,
            progress_callback=None)
        print(response)

    def delete(self, osskey):
        self.client.delete_object(
            Bucket='personal-documents-1309903013',
            Key=osskey
        )

    def get_download_url(self, osskey):
        url = self.client.get_presigned_url(
            Method='GET',
            Bucket='personal-documents-1309903013',
            Key=osskey,
            Params={
                'response-content-disposition': f'attachment; filename={osskey}'  # 下载时保存为指定的文件
            },
            Expired=120  # 120秒后过期，过期时间请根据自身场景定义
        )
        return url

    def get_preview_url(self, osskey):
        url = self.client.get_presigned_url(
            Method='GET',
            Bucket='personal-documents-1309903013',
            Key=osskey,
            Params={
                'response-content-disposition': f'attachment; filename={osskey}'  # 下载时保存为指定的文件
                # 除了 response-content-disposition，还支持 response-cache-control、response-content-encoding、response-content-language、
                # response-content-type、response-expires 等请求参数，详见下载对象 API，https://cloud.tencent.com/document/product/436/7753
            },
            Expired=120  # 120秒后过期，过期时间请根据自身场景定义
        )
        return url

    def get_token(self):
        scopes = list()
        scopes.append(CIScope('*', 'personal-documents-1309903013', 'ap-nanjing', '*'))
        scopes.append(Scope('*', 'personal-documents-1309903013', 'ap-nanjing', '*'))
        config = {
            'sts_scheme': 'http',
            'sts_url': 'sts.tencentcloudapi.com/',
            # 临时密钥有效时长，单位是秒
            'duration_seconds': 60,
            'secret_id': self.secret_id,
            # 固定密钥
            'secret_key': self.secret_key,
            # 换成 bucket 所在地区
            'region': 'ap-nanjing',
            #  设置网络代理
            # 'proxy': {
            #     'http': 'xxx',
            #     'https': 'xxx'
            # },
            'policy': Sts.get_policy(scopes)
        }

        sts = Sts(config)
        response = sts.get_credential()
        res = dict(response)
        return res


if __name__ == '__main__':
    bot = TencentOss()
    url = bot.get_download_url('1719391023_副本f2d1de9f-bd6d-49ce-98a9-8156ffcc0d12_xiangmuxinxi-2.docx')
    print(url)
    # url = bot.get_token('1694966149_150.png')
    # print(url)
